/**
 * classe responsavel pela criacao das fases no game 
 */
import corejuliano.gameelement.world.*;
import corejuliano.gameelement.perspective.*;
import corejuliano.designpattern.mvc.*;
import corejuliano.designpattern.observer.*;
import corejuliano.gameelement.*;
import corejuliano.*;

import util.*;
import flash.geom.*;
 
class corejuliano.Phase
{
	private var world:World;
	private var canvasTopDown:MovieClip;
	private var canvasIsometric:MovieClip;
	
	public function Phase()
	{
		
	}
	
	public function updateLogic():Void
	{
		world.notifyObservers(GameManager.UPDATE_LOGIC);
		    
  		for(var i:Number = 0; i < world.getMapTiles().length; i++)
  		{
   			var tile:Tile = Tile(world.getMapTiles()[i]);   				
   				for (var j:Number = tile.getTileables().length-1; j >= 0; j--)
    				tile.getTileables()[j].notifyObservers(GameManager.UPDATE_LOGIC);
		}		
	}		
		
	public function updateScreen():Void
	{
		world.notifyObservers(GameManager.UPDATE_SCREEN);
		  
  		for(var i:Number = 0; i < world.getMapTiles().length; i++)
  		{
   			var tile:Tile = Tile(world.getMapTiles()[i]);
   			for (var j:Number = tile.getTileables().length-1; j >= 0; j--)
    			tile.getTileables()[j].notifyObservers(GameManager.UPDATE_SCREEN);
  		}  
	}
	
	public function start():Void
	{
		canvasTopDown = _root.createEmptyMovieClip("canvastopdown",1);
		canvasTopDown._xscale = 100;
		canvasTopDown._yscale = 100;
		/*
		canvasIsometric = _root.createEmptyMovieClip("canvasisometric",2);
		canvasIsometric._xscale = 50;
		canvasIsometric._yscale = 50;
		*/
	}
	
	public function startEspecifyPhase():Void
	{
		
	}
	
	public function createWorld(id:String, xml:String, way:String, spacing:Number):Void
	{
		world = new World (id, xml, way, spacing);
		world.addObserver(new WorldTopDownView (canvasTopDown, world.getModel()));
	}
	
	public function createWall(id:String, tile:Tile, canvas:MovieClip):Void
	{		
		var wall:Wall = new Wall(id);
		wall.addObserver (new View (world.getWorldTopDownCanvas(), wall.getModel()));
		TileableModel(wall.getModel()).setZIndex(1);
		wall.setTile(tile);
		wall.setSolid(true);
	}
	
	public function createItem (id:String, tile:Tile):Void
	{
		var item:Item = new Item(id);
		item.addObserver (new View (world.getWorldTopDownCanvas(), item.getModel()));
		TileableModel(item.getModel()).setZIndex(0);		
		item.setSolid(false);
		item.setTile(tile);
		
	}
	
	public function getWorld():World
	{
		return world;
	}
	
	public function toString():String 
	{
		return "corejuliano.Phase";
	}
}